package com.chee.plant.hibernate.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.chee.plant.hibernate.object.Equipment;
import com.chee.plant.server.HibernateSessionFactory;

/**
 * 对应Equipment数据库访问操作
 * 
 * @author xue cb
 * 
 */
public class EquipmentDAO {

	/**
	 * 根据Id得到装备信息
	 * 
	 * @return 指定Id的装备实例
	 */
	public Equipment getEquipById(int id) {
		Session session = HibernateSessionFactory.getSession();
		String hql = "select  equipment from Equipment as equipment where equipment.Id=:id";
		Query query = session.createQuery(hql);
		query.setInteger("id", id);
		return (Equipment) query.uniqueResult();
	}

	/**
	 * 获取所有可以从npc购买到的装备，数据库中0代表可以从npc买到，1代表不可以从npc买到。
	 * 
	 * @return 可以从买到的装备列表
	 */
	@SuppressWarnings("unchecked")
	public List<Equipment> getAllEquipment() {
		Session session = HibernateSessionFactory.getSession();
		String hql = "select equipment from Equipment as equipment where equipment.NpcSell=0";
		Query query = session.createQuery(hql);
		List<Equipment> list = query.list();
		return list;
	}

	/**
	 * 主方法，用于测试
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		// 1, 测试getAllEquipment()
		List<Equipment> list = new EquipmentDAO().getAllEquipment();
		System.out.println(list.size());
		// 2, 测试getEquipById(int id)
		Equipment equip = new EquipmentDAO().getEquipById(1);
		System.out.println(equip.getDesc());

	}
}
